package cn.jly.flink.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.concurrent.atomic.AtomicLong;

/**
 * @PackageName cn.jly.flink.utils
 * @ClassName Constants
 * @Description 常量
 * @Author 姬岚洋
 * @Date 2021/1/8 下午 4:43
 */
public interface ConstantUtils {

    AtomicLong ID_GENERATOR = new AtomicLong(1001);

    interface Kafka {
        String BOOTSTRAP_SERVERS_VALUE = "node01:9092,node02:9092,node03:9092";
    }

    interface Mysql {
        String JDBC_URL = "jdbc:mysql://localhost:3306/test";
        String USERNAME = "root";
        String PASSWORD = "123456";
        String DRIVER_NAME = "com.mysql.jdbc.Driver";
        /**
         * 获取连接
         *
         * @return
         */
        static Connection getConnection() {
            Connection connection = null;
            try {
                Class.forName(DRIVER_NAME);
                connection = DriverManager.getConnection(JDBC_URL, USERNAME, PASSWORD);
            } catch (Exception e) {
                e.printStackTrace();
            }

            return connection;
        }

        /**
         * 释放资源
         *
         * @param preparedStatement
         * @param connection
         */
        static void release(PreparedStatement preparedStatement, Connection connection) {
            if (preparedStatement != null) {
                try {
                    preparedStatement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
